$(function () {
    // 获取拍品
    // var loading = weui.loading('loading', {
    //     className: 'custom-classname'
    // });
    var result = Model.goods.findMyAllGoods();
    // if (!sessionStorage.getItem('hmdj_stopError')) {
    //     sessionStorage.setItem('hmdj_stopError', true);
    // } else {
    //     if (result.code) {
    //         Common.fn.formatOutput(result);
    //         loading.hide();
    //         return false;
    //     }
    // }
    for (var i = 0; i < result.data.length; i++) {
        var item = ' <div class="weui-cell manager-list" >' +
            '                <img class="pruduct-img" src="' + ((result.data[i].goodsImg.length > 0) ? result.data[i].goodsImg[0].image : '') + '" goods-id="' + result.data[i].id + '">' +
            '                <div class="weui-cell__hd">' +
            '                    <h3 class="text-1-6">' + result.data[i].content + '</h3>' +
            '                    <p class="patron-price">库存：<span class="stock">' + result.data[i].stock + '</span></p>';
        switch (result.data[i].state) {
            case 1:
                item += '<a href="javascript:;" class="weui-btn weui-btn_mini weui-btn_default change-state-btn" goods-id="' + result.data[i].id + '" goods-state="0" >下架</a>';
                break;
            case 0:
                item += '<a href="javascript:;" class="weui-btn weui-btn_mini weui-btn_default change-state-btn" goods-id="' + result.data[i].id + '" goods-state="1">重新上架</a>';
                break;
        }

        item += '                    <a href="javascript:;" class="weui-btn weui-btn_mini weui-btn_default edit-btn" goods-id="' + result.data[i].id + '"  goods-stock="' + result.data[i].stock + '">编辑库存</a>' +
            '                    <a href="javascript:;" class="weui-btn weui-btn_mini weui-btn_default del-product-btn" goods-id="' + result.data[i].id + '">删除</a>' +
            '                </div>' +
            '            </div>';

        $('.pruduct-manager').append(item);
    }
    // loading.hide();
});

$(document).on('mousedown', '.change-state-btn', function () {
    var id = $(this).attr('goods-id'),
        state = $(this).attr('goods-state'),
        title = '';
    state == 0 ? title = '下架' : title = '重新上架';
    var confirms = weui.confirm(
        "真的要" + title + "吗？",
        function () {
            // 上下架管理
            var result = Model.goods.modifyGoodsItemState({
                goodsId: id,
                state: state
            });
            if (result.code) {
                location.reload();
                return false;
            }
            location.reload();
        }
    );
});

$(document).on('mousedown', '.del-product-btn', function () {
    var id = $(this).attr('goods-id');
    var confirms = weui.confirm(
        "真的要删除该拍品吗？",
        function () {
            // 删除拍品
            var result = Model.user.delGoods({
                goodsId: id,
            });
            if (result.code) {
                location.reload();
                return false;
            }
            location.reload();
        },
        function () {
        }
    );
});
// 编辑库存
$(document).on('click', '.edit-btn', function () {
    var id = $(this).attr('goods-id'),
        stock = $(this).attr('goods-stock');
    weui.confirm(
        "请输入库存<input type='number' id='edit-stock-input' style='font-size: 2rem; width: 80%;' value='" + stock + "' min=1 max=999>",
        function () {
            // 输入判断
            var edit_stock = $("#edit-stock-input").val();
            if (edit_stock == '' || parseInt(edit_stock) <= 0 || parseInt(edit_stock) > 999) {
                alert('数字不符合规范');
                return false;
            }
            // 编辑库存
            result = Model.goods.updateStock({
                goodsId: id,
                stock: edit_stock
            });
            location.reload();
            if (result.code) {
                Common.fn.formatOutput(result);
                return false;
            }

        }
    );
});
// 页面跳转
$(document).on('click', '.pruduct-img', function () {
    location.href = 'goods_detail.html?id=' + $(this).attr('goods-id');
});